package com.bsth.archive;

import java.security.MessageDigest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


public class PasswdUtil {

	private final static String[] hexDigits = {
			"0", "1", "2", "3", "4", "5", "6", "7",
			"8", "9", "a", "b", "c", "d", "e", "f"};
	// 连接数据库
	private String DB_URL = "jdbc:oracle:thin:@192.68.16.8:1521:orcl";
	private String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
	private String DB_USERNAME = "archive";
	private String DB_PASSWORD = "cjpdwxpdda2010_";

	/**
	 * 转换字节数组为16进制字串
	 *
	 * @param b 字节数组
	 * @return 16进制字串
	 */

	public static String byteArrayToHexString(byte[] b) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			resultSb.append(byteToHexString(b[i]));
		}
		return resultSb.toString();
	}

	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0)
			n = 256 + n;
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}

	public static String MD5Encode(String origin) {
		String resultString = null;

		try {
			resultString = new String(origin);
			MessageDigest md = MessageDigest.getInstance("MD5");
			resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
		} catch (Exception ex) {

		}
		return resultString;
	}

	public static void main(String args[]) {
		PasswdUtil test = new PasswdUtil();
		//test.StrToChar(pass);
		test.serchData();
	}

	/******************
	 * start以下是二进制转换密码的,和分级系统保持一致
	 *************************/
	public char BinstrToChar(String binStr) {
		int[] temp = BinstrToIntArray(binStr);
		int sum = 0;
		for (int i = 0; i < temp.length; i++) {
			sum += temp[temp.length - 1 - i] << i;
		}
		return (char) sum;
	}

	public int[] BinstrToIntArray(String binStr) {
		char[] temp = binStr.toCharArray();
		int[] result = new int[temp.length];
		for (int i = 0; i < temp.length; i++) {
			result[i] = temp[i] - 48;
		}
		return result;
	}

	public Connection getConnection() {
		/** 声明Connection连接对象 */
		Connection conn = null;
		try {
			Class.forName(DB_DRIVER);
			conn = DriverManager
					.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	public String serchData() {
		String result = "";
		try {
			Connection conn = getConnection();
			Statement stmt = conn.createStatement();
			String sqlStr = "select * from BSTH_SYS_USER"; //+"  where id=31";
			System.out.println(sqlStr);
			ResultSet rset = stmt.executeQuery(sqlStr);
			PasswdUtil test = new PasswdUtil();
			String password = "";
			System.out.println("查询结果:");
			int i = 0;
			while (rset.next()) {
				System.out
						.println("id:" + rset.getString("ID").toString()
								+ "               用户名："
								+ rset.getString("REALNAME").toString()
								+ "        登录名："
								+ rset.getString("LOGINNAME").toString()
								+ "        密码："
								+ rset.getString("PASSWORD").toString());
				result = rset.getString("PASSWORD").toString().trim();
				i++;
				//System.out.print(result);

				// 解密
				 /*password =
				 test.charToStr(rset.getString("PASSWORD").toString());*/

				// 加密
				/*password = test
						.StrToChar(rset.getString("PASSWORD").toString());

				test.updData(Integer.parseInt(rset.getString("ID")), password);*/

			}
			System.out.println("总数= " + i);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;

	}

	public void updData(int id, String password) {
		try {
			Connection conn = getConnection();
			Statement stmt = conn.createStatement();
			String sqlStr = "update BSTH_SYS_USER set password='" + password
					+ "' where id=" + id;
			stmt.execute(sqlStr);
			String sqlStr2 = "select * from BSTH_SYS_USER where id=" + id;
			ResultSet rset = stmt.executeQuery(sqlStr2);
			System.out.println("修改后的结果:");
			while (rset.next()) {
				System.out
						.println("id:" + rset.getString("ID").toString()
								+ "               用户名："
								+ rset.getString("REALNAME").toString()
								+ "        登录名："
								+ rset.getString("LOGINNAME").toString()
								+ "        密码："
								+ rset.getString("PASSWORD").toString());

			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 字符转换二进制
	public String StrToChar(String pass) {
		// 生成二进制字符串
		char[] strChar = pass.toCharArray();
		String result = "";
		for (int i = 0; i < strChar.length; i++) {
			result += Integer.toBinaryString(strChar[i]) + " ";
		}
		return result;
	}

	public String charToStr(String pass) {
		PasswdUtil test = new PasswdUtil();
		// 二进制转成字符串
		String[] tempStr = pass.split(" ");
		char[] tempChar = new char[tempStr.length];
		String ss = "";
		for (int i = 0; i < tempStr.length; i++) {
			tempChar[i] = test.BinstrToChar(tempStr[i]);
			ss += tempChar[i];
		}
		// System.out.println("字符串："+ss);
		return ss;
	}

	/******************end以下是二进制转换密码的,和分级系统保持一致*************************/

}
